package com.ruoyi.web.controller.admin;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.BdmOrder;
import com.ruoyi.system.domain.BdmOrderPhysicalDistributionVo;
import com.ruoyi.system.domain.dto.BdmOrderDto;
import com.ruoyi.system.domain.vo.BdmOrderVo;
import com.ruoyi.system.domain.vo.applet.AppletOrderDetailsVo;
import com.ruoyi.system.mapper.BdmUserMapper;
import com.ruoyi.system.service.IBdmOrderService;
import com.ruoyi.system.service.IDistributionService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2024-12-13
 */
@Api(tags = "后台-订单管理")
@RestController
@RequestMapping("/admin/order")
@Validated
public class BdmOrderController extends BaseController {

    @Autowired
    private IBdmOrderService bdmOrderService;

    @Autowired
    private IDistributionService iDistributionService;
    @Autowired
    private BdmUserMapper bdmUserMapper;



    /**
     * 查询订单列表
     */
    @GetMapping("/selectPageOrder")
    public TableDataInfo selectPageOrder(BdmOrderDto dto) {
        startPage();
        List<BdmOrderVo> list = bdmOrderService.selectPageOrder(dto);

      //  BdmOrder bdmOrder = new BdmOrder();
     //   bdmOrder.setCreateTime(DateUtils.parseDate("2025-07-11 10:59:42"));
    //    List<BdmOrder>  list1= bdmOrderService.lambdaQuery().ge(BdmOrder::getCreateTime,bdmOrder.getCreateTime()).orderByDesc(BdmOrder::getCreateTime).list();
       // String token=getAccessToken("wx8ee7c46e3f57fa80","4881a7e85282e0238074cde855e16f1f");
//        String token= "95_hieSuN3ASJB4mcGKNZjv7NeN1jbqQqW3p6nEMjW2PRcVWaFJUhpkkXRjc_kYYPxZUe4EhgPJn1pP7KQXDS1_CIm5XNqx9i2codZc43UdcbfOanYcCa8cAo4FHEEDHVdAAARVJ";
//        System.out.println(token);
//         for(int i=0;i<list1.size();i++){
//             try {
//                 bdmOrder=list1.get(i);
//                // JSONObject object = searchWxID(bdmOrder.getId());
//               //  if(object!=null){
//                   //  bdmOrder.setFakeId(object.getString("fakeId"));
//                  //   bdmOrder.setNativeId(object.getString("nativeId"));
//                 if(!ObjectUtils.isEmpty(bdmOrder.getFakeId())){
//                     BdmUserVo bdmUserVo= bdmUserMapper.getBdmUserByUserId(bdmOrder.getUserId());
//                     //  bdmOrderService.updateBdmOrder(bdmOrder);
//                    // sendGood(token, bdmOrder, bdmUserVo.getOpenId());
//                 }
//
//               //  }
//
//             } catch (Exception e) {
//                 e.printStackTrace();
//             }
//         }

        return getDataTable(list);
    }









    /**
     * 查询订单详情
     * @return {@link AppletOrderDetailsVo}
     */
    @GetMapping("/orderDetails")
    public AjaxResult orderDetails(@NotBlank(message = "订单号必填") String orderId)
    {
        return success(bdmOrderService.orderDetails(orderId));
    }


    /**
     * 根据订单号查询订单轨迹
     */
    @GetMapping("/getOrderPhysicalDistribution")
    public TableDataInfo getOrderPhysicalDistribution(@RequestParam("orderId") String orderId) {
        BdmOrder bdmOrder = bdmOrderService.getById(orderId);
        HashMap<String, String> map = new HashMap<>();
        map.put("shipperCode", bdmOrder.getLcId());
        map.put("logisticCode", bdmOrder.getLId());
        List<BdmOrderPhysicalDistributionVo> list = iDistributionService.getOrderPhysicalDistribution(map);
        if(list!=null){
            return getDataTable(iDistributionService.getOrderPhysicalDistribution(map).stream().sorted(Comparator.comparing(BdmOrderPhysicalDistributionVo::getAcceptTime).reversed()).collect(Collectors.toList()));
        }else{
            return  getDataTable(new ArrayList<BdmOrderPhysicalDistributionVo>());
        }
    }


    /**
     * 修改订单  发货
     */
    @PutMapping
    public AjaxResult put(@RequestBody BdmOrder bdmOrder) throws Exception {
        return toAjax(bdmOrderService.updateBdmOrder1(bdmOrder));
    }

    /**
     * 删除订单
     */
    @PostMapping("/deleteOrder")
    public AjaxResult deleteOrder(@RequestBody BdmOrderDto dto)
    {
        return toAjax(bdmOrderService.removeBatchByIds(dto.getIds()));
    }

    @GetMapping("/testQueryOrder")
    public AjaxResult testQueryOrder()
    {
        iDistributionService.shipmentsQuery();
        return success(1);
    }

}
